<?php

    defined('_VALID_MOS') or die(include("404.php"));

    class Pager
    {

        static function getPagerData($numHits, $limit, $page)
        {
            //kiem tra du lieu nhap
            $numHits = (int) $numHits; //tong so mau tin
            $limit = max((int) $limit, 1); //so mau tin moi trang
            $page = (int) $page; //so trang dang duyet
            $numPages = ceil($numHits / $limit); // tinh tong so trang
            $page = max($page, 1); //neu page < 1 thì page = 1
            $page = min($page, $numPages); //neu page>$numPages thì page=$numPages
            $offset = ($page - 1) * $limit;
            $paging = "";

            $selfUrl = '';

            $view = '';
            if (isset($_GET["view"]))
            {
                $view = $_GET["view"];
            }


            if ($view == "category")
            {
                $selfUrl = "./com_content/category/" . $_GET["alias"] . "/" . $_GET["item"];
            }
            else if ($view == "session")
            {
                $selfUrl = "./com_content/session/" . $_GET["alias"] . "/" . $_GET["item"];
            }
            else if ($view == "article")
            {
                $selfUrl = "./com_content/article/" . $_GET["alias"] . "/" . $_GET["item"];
            }
            else if ($view == "solved_question")
            {
                $selfUrl = "./com_question_answer/" . $view . "/" . $_GET["alias"] . "/" . $_GET["item"];
            }
            else if ($view == "processing_question")
            {
                $selfUrl = "./com_question_answer/" . $view . "/" . $_GET["alias"] . "/" . $_GET["item"];
            }
            else if ($view == "mail")
            {
                $selfUrl = "./com_mail/mail/" . $_GET["alias"] . "/" . $_GET["item"];
            }
            else if ($view == "question_of_canbo")
            {
                $selfUrl = "./com_question_of_canbo/" . $view . "/" . $_GET["alias"] . "/" . $_GET["item"];
            }




            $maxP = 2;
            $revpage = $page - 1;
            $nexpage = $page + 1;

            $page = $page;
            $pages = $numPages;
            $hien_tai = $page;
            $bien_trai = $hien_tai - $maxP;
            $bien_phai = $hien_tai + $maxP;


            switch ($view)
            {
                case 'solved_question':
                case "processing_question":
                    if ($page == 1 || $page == 0)
                    {
                        $paging .= "<li class = \"page_text\"><a href=\"javascript:void(0);\"> <img src=\"templates/default/images/FirstDisabled.jpg\"> </a></li>";
                        $paging .= "<li class=\"page_text\"><a href=\"javascript:void(0);\"> <img src=\"templates/default/images/PreviousDisabled.jpg\"> </a></li>";
                    }
                    else
                    {
                        $paging = $paging . "<li class=\"page_text\"><a onClick=\"submitForm('transferPage','');\" href=\"$selfUrl/1.csp\" title=\"Trang đầu\"><img src=\"templates/default/images/First.jpg\"></a></li>";
                        $paging = $paging . "<li class=\"page_text\"><a onClick=\"submitForm('transferPage','');\" href=\"$selfUrl/$revpage.csp\" title=\"Trang kế\"><img src=\"templates/default/images/Previous.jpg\"></a></li>";
                    }



                    if ($bien_trai < 1) $bien_trai = 1;

                    if ($bien_phai > $pages) $bien_phai = $pages;

                    if ($bien_trai > 1) $paging = $paging . "<li class=\"page_img\"><a onClick=\"submitForm('transferPage','');\" href=\"$selfUrl/1.csp\">1 </a></li>";

                    if ($bien_trai > 2) $paging = $paging . " <li class=\"page_text\"> ... </li> ";

                    for ($i = $bien_trai; $i <= $bien_phai; $i++)
                    {
                        if ($i == $hien_tai) $paging = $paging . "<li class=\"page_active\"><a onClick=\"submitForm('transferPage','');\" href=\"javascript:void(0)\">$i </a></li>";
                        else $paging = $paging . "<li class=\"page_img\"><a onClick=\"submitForm('transferPage','');\" href=\"$selfUrl/$i.csp\">$i </a></li>";
                    }

                    if ($bien_phai < $pages) $paging = $paging . " <li class=\"page_text\"> ... </li> ";

                    if ($bien_phai < $pages - 1) $paging = $paging . "<li class=\"page_img\"><a onClick=\"submitForm('transferPage','');\" href=\"$selfUrl/$pages.csp\">$pages</a></li>";

                    if ($page == $pages)
                    {
                        $paging = $paging . "<li class=\"page_text\"><a onClick=\"submitForm('transferPage','');\" href=\"javascript:void(0);\"><img src=\"templates/default/images/NextDisabled.jpg\"></a></li>";
                        $paging = $paging . "<li class=\"page_text\"><a onClick=\"submitForm('transferPage','');\" href=\"javascript:void(0);\"><img src=\"templates/default/images/LastDisabled.jpg\"></a></li>";
                    }
                    else
                    {
                        $paging = $paging . "<li class=\"page_text\"><a onClick=\"submitForm('transferPage','');\" href=\"$selfUrl/$nexpage.csp\" title=\"Trang sau\"><img src=\"templates/default/images/Next.jpg\"></a></li>";
                        $paging = $paging . "<li class=\"page_text\"><a onClick=\":submitForm('transferPage','');\" href=\"$selfUrl/$pages.csp\" title=\"Trang cuối\"><img src=\"templates/default/images/Last.jpg\"></a></li>";
                    }
                    break;
                default :
                    if ($page == 1 || $page == 0)
                    {
                        //$paging .= "<li class = \"page_text\"><a href=\"javascript:void(0);\"> <img src=\"templates/default/images/FirstDisabled.jpg\"> </a></li>";
                        //$paging .= "<li class=\"page_text\"><a href=\"javascript:void(0);\"> <img src=\"templates/default/images/PreviousDisabled.jpg\"> </a></li>";
                    }
                    else
                    {
                       // $paging = $paging . "<li class=\"page_text\"><a href=\"$selfUrl/1.csp\" title=\"Trang đầu\"><img src=\"templates/default/images/First.jpg\"></a></li>";
                        $paging = $paging . "<a href=\"$selfUrl/$revpage.csp\" class=\"pagination_btn\"  title=\"Trang kế\"><img src=\"templates/default/images/pagination_prev.gif\"></a>";
                    


                    }



                    if ($bien_trai < 1) $bien_trai = 1;

                    if ($bien_phai > $pages) $bien_phai = $pages;

                    if ($bien_trai > 1) $paging = $paging . "<a href=\"$selfUrl/1.csp\">1 </a>";

                    if ($bien_trai > 2) $paging = $paging . " <li class=\"page_text\"> ... </li> ";

                    for ($i = $bien_trai; $i <= $bien_phai; $i++)
                    {
                        if ($i == $hien_tai) $paging = $paging . "<a href=\"javascript:void(0)\">$i </a>";
                        else $paging = $paging . "<a href=\"$selfUrl/$i.csp\">$i </a>";
                    }

                    if ($bien_phai < $pages) $paging = $paging . " <li class=\"page_text\"> ... </li> ";

                    if ($bien_phai < $pages - 1) $paging = $paging . "<a href=\"$selfUrl/$pages.csp\">$pages</a>";

                    if ($page == $pages)
                    {
                        //$paging = $paging . "<li class=\"page_text\"><a href=\"javascript:void(0);\"><img src=\"templates/default/images/NextDisabled.jpg\"></a></li>";
                        //$paging = $paging . "<li class=\"page_text\"><a href=\"javascript:void(0);\"><img src=\"templates/default/images/LastDisabled.jpg\"></a></li>";
                    }
                    else
                    {
                        $paging = $paging . "<a class=\"pagination_btn\" href=\"$selfUrl/$nexpage.csp\" title=\"Trang sau\"><img src=\"templates/default/images/pagination_next.gif\"></a>";
                       // $paging = $paging . "<li class=\"page_text\"><a href=\"$selfUrl/$pages.csp\" title=\"Trang cuối\"><img src=\"templates/default/images/Last.jpg\"></a></li>";
                    }
                    break;
            }




            $ret = new stdClass();
            $ret->offset = $offset;
            $ret->limit = $limit + $offset;
            $ret->numPages = $numPages;
            $ret->page = $page;
            $ret->paging = $paging;
            return $ret;

        }

    }
?> 